package cn.bj.yu.cas;

import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceDemo {
    public static void main(String[] args) {
        User zhangsan = new User("zhangsan", 22);
        User lisi = new User("lisi", 25);

        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(zhangsan);//设置

        System.out.println(atomicReference.compareAndSet(zhangsan, lisi));
        System.out.println(atomicReference.get().toString());

        System.out.print(atomicReference.compareAndSet(zhangsan, lisi));
        System.out.println("\n" + atomicReference.get().toString());
    }
}

class User {
    String username;
    int age;

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}
